package com.moli.iov.sys.remote.impl;

import com.moli.iov.exception.BusinessException;
import com.moli.iov.rest.RestResponse;
import com.moli.iov.sys.constants.Constant;
import com.moli.iov.sys.enums.CompanyStatusEnum;
import com.moli.iov.sys.enums.EnVisibleEnum;
import com.moli.iov.sys.enums.UserTypeEnum;
import com.moli.iov.sys.model.dto.req.CompanyDto;
import com.moli.iov.sys.model.dto.req.PermissionDto;
import com.moli.iov.sys.model.dto.req.PermissionNeApiDto;
import com.moli.iov.sys.model.po.Company;
import com.moli.iov.sys.model.po.Permission;
import com.moli.iov.sys.remote.IPermissionRemoteService;
import com.moli.iov.sys.service.ICompanyService;
import com.moli.iov.sys.service.IPermissionService;
import com.moli.iov.sys.service.IRolePermissionService;
import com.moli.iov.sys.service.IRoleService;
import com.moli.iov.sys.service.IUserRoleService;
import java.util.Collections;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 权限业务
 * </p>
 *
 * @author zql
 * @since 2019/9/11 16:48
 */
@Slf4j
@Service(group = "${service.group}")
public class PermissionRemoteServiceImpl implements IPermissionRemoteService {

  @Autowired
  private IUserRoleService userRoleService;

  @Autowired
  private IRolePermissionService rolePermissionService;

  @Autowired
  private IPermissionService permissionService;

  @Autowired
  private IRoleService roleService;

  @Autowired
  private ICompanyService companyService;

  /**
   * 获取用户api、btn权限
   * @param userId
   * @return
   */
  @Override
  public List<Permission> listApiAndBtnPermission(Long userId) {

    // 获取角色id 集合
    List<Long> roleIds = userRoleService.listRoleIdsByUserId(userId);

    if(roleIds == null || roleIds.isEmpty()){
      return Collections.emptyList();
    }

    // 获取权限id 集合
    List<Long> permissionIds = rolePermissionService.listPermissionIds(roleIds);

    return permissionService.listApiAndBtnPermissions(permissionIds);
  }

  /**
   * 获取用户菜单、按钮权限 递归
   * @param userId
   * @return
   */
  @Override
  public List<Permission> listTreePermission(Long userId){
    // 获取角色id 集合
    List<Long> roleIds = userRoleService.listRoleIdsByUserId(userId);

    if(roleIds == null || roleIds.isEmpty()){
      return Collections.emptyList();
    }
    // 获取权限id 集合
    List<Long> permissionIds = rolePermissionService.listPermissionIds(roleIds);

    if(permissionIds == null || permissionIds.isEmpty()){
      return Collections.emptyList();
    }

    PermissionNeApiDto permissionNeApiDto = new PermissionNeApiDto();
    permissionNeApiDto.setPid(Constant.PID);
    permissionNeApiDto.setPermissionIds(permissionIds);
    return permissionService.listPermissionNeApi(permissionNeApiDto);
  }

  /**
   * 获取超级管理员菜单、按钮权限 超级管理员
   * @param userType 用户类型
   * @return
   */
  @Override
  public List<Permission> listTreePermissionOfSuperAdmin(Integer userType) {

    PermissionNeApiDto permissionNeApiDto = new PermissionNeApiDto();
    permissionNeApiDto.setPid(Constant.PID);
    if(UserTypeEnum.COMPANY.getFlag() == userType){
      permissionNeApiDto.setEnVisible(EnVisibleEnum.ENABLED.getFlag());
    }
    return permissionService.listPermissionNeApi(permissionNeApiDto);
  }

  /**
   * 校验企业
   * @param companyId
   */
  private Company companyValid(Long companyId){
    CompanyDto companyDto = new CompanyDto();
    companyDto.setCompanyId(companyId);
    List<Company> companies = companyService.listCompany(companyDto);
    if(companies == null || companies.isEmpty()){
      throw new BusinessException("企业id不存在");
    }

    Company company = companies.get(0);
    if(company.getStatus() == CompanyStatusEnum.DISABLED.getFlag()){
      throw new BusinessException("企业已停用");
    }

    return company;
  }

  /**
   * 根据用户类型查询全部权限
   * @param permissionDto
   * @return
   */
  @Override
  public RestResponse<List<Permission>> listAllPermissionNeApi(PermissionDto permissionDto) {

    PermissionNeApiDto permissionNeApiDto = new PermissionNeApiDto();
    permissionNeApiDto.setPid(Constant.PID);

    if(UserTypeEnum.COMPANY.getFlag() == permissionDto.getSysUserType()){
      permissionNeApiDto.setEnVisible(EnVisibleEnum.ENABLED.getFlag());
    }

    List<Permission> permissions = permissionService.listPermissionNeApi(permissionNeApiDto);
    return RestResponse.success(permissions);
  }
}
